package Function;

import Function.Utils.FCFileCopyToMaterial;
import VWorldGameStudio.FCItem;
import com.google.gson.Gson;
import lombok.Getter;
import utils.FCDataSingles;

import java.io.File;
import java.util.HashMap;
import java.util.Vector;

public class FCItemLibrary {
    public interface ExtraItemCloneImpl {
        FCItem getCloneFcItem(String itemId);
    }

    private Vector<FCItem> itemList = new Vector<FCItem>();
    @Getter
    private Vector<String> itemName = new Vector<String>();
    private FCFileCopyToMaterial fileCopy = new FCFileCopyToMaterial();
    private static final String ITEM_LIST_PATH = "./data/itemlist.vw";
    private static final String ITEMS_PATH = "./data/item";
    private int itemallnum = 0;

    private HashMap<String, FCItem> itemMaps = new HashMap<>();

    private ExtraItemCloneImpl extraItemCloneImpl;

    /**
     * 初始化物品类，读取数量
     * 建议游戏启动方使用
     *
     * @date 2021-11-30
     */
    public FCItemLibrary() {
        try {
            this.itemallnum = new File(ITEMS_PATH).listFiles().length;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 物品数据加载函数
     *
     * @date 2021-11-30
     */
    public void loadItemData() {
        try {
            Gson gson = new Gson();
            File[] f = new File(ITEMS_PATH).listFiles();
            for (int i = 0; i < itemallnum; i++) {
                FCItem item = FCDataSingles.getReader().readItemData(i, f[i].getPath());
                itemName.add(item.getName());
                itemList.add(item);
                itemMaps.put(item.getName(), item);
                System.out.println(gson.toJson(item));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取物品实际数据量
     *
     * @return integer
     * @date 2021-11-30
     */
    public int getItemAllNum() {
        return itemallnum;
    }

    /**
     * 初始化物品数据
     *
     * @param isGame
     */
    public FCItemLibrary(boolean isGame) {
        Gson gson = new Gson();
        File[] f = new File(ITEMS_PATH).listFiles();
        for (int i = 0; i < f.length; i++) {
            try {
                FCItem item = FCDataSingles.getReader().readItemData(i, f[i].getPath());
                itemName.add(item.getName());
                itemList.add(item);
                itemMaps.put(item.getName(), item);
                System.out.println(gson.toJson(item));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    // 注册物品
    public void registerItem(boolean isnew, int ID, String name, String catalog, String type, String occupation,
                             int attackHurt, int attackNum, int bloodAdd, int bloodNum, int bloodRound, int defenseAdd,
                             int defenseNum, int defernseRound, int subdefense, int subdefenseNum, int subdefenseRound, int desdefense,
                             int desdefenseNum, int elementsNum, int elementsRound, String UpgradeTarget, int elementsValue,
                             double crit, String elements, double capacity, String eventID, String pngurl, double zhongliang,
                             String ani, int[] dipanxue, int shellcasenum, int repairPrice, int fillPrice, int upgradeMoney,
                            String depictText, String relateMonster) {
//        FCItem qw = new FCItem();
//        if (ani != null && ani.equals("null")) {
//            ani = null;
//        }

//        qw.registerItem(ID, name, catalog, type, occupation, attackHurt, attackNum, bloodAdd, bloodNum,
//                bloodRound, defenseAdd, defenseNum, defernseRound, subdefense, subdefenseNum, subdefenseRound,
//                desdefense, desdefenseNum, elementsNum, elementsRound, UpgradeTarget, elementsValue, crit,
//                elements, capacity, eventID,
//                fileCopy.copyPngToMaterialPng(3, pngurl), zhongliang, ani, dipanxue, shellcasenum);
//        qw.registerItemFix(repairPrice, fillPrice, upgradeMoney, depictText, relateMonster);
//        Gson gson = new Gson();
//        System.out.println("编辑 " + gson.toJson(qw));
//        if (isnew) {
//            itemList.add(qw);
//            itemName.add(name);
//            itemMaps.put(name, qw);
//        } else {
//            itemList.remove(ID);
//            itemName.remove(ID);
//            itemMaps.remove(name);
//            itemList.add(ID, qw);
//            itemName.add(ID, name);
//            itemMaps.put(name, qw);
//        }
//        FCDataSingles.getWriter().writeItemData(qw);
    }

    // 获取物品最新ID
    public int getItemNewID() {
        return itemName.size();
    }


    public int getIdByName(String name) {
        return itemName.indexOf(name);
    }

    // 获取所有主炮id
    public Vector<String> getGun1ID() {
        Vector<String> id = new Vector<>();
        for (int i = 0; i < itemName.size(); i++)
            if (itemList.get(i).getType().equals("gun1"))
                id.add(itemName.get(i));
        return id;
    }

    // 获取所有副炮id
    public Vector<String> getGun2ID() {
        Vector<String> id = new Vector<>();
        for (int i = 0; i < itemName.size(); i++)
            if (itemList.get(i).getType().equals("gun2"))
                id.add(itemName.get(i));
        return id;
    }

    // 获取所有发动机id
    public Vector<String> getEngineID() {
        Vector<String> id = new Vector<>();
        for (int i = 0; i < itemName.size(); i++)
            if (itemList.get(i).getType().equals("engine"))
                id.add(itemName.get(i));
        return id;
    }

    // 获取所有C装置id
    public Vector<String> getCdeviceID() {
        Vector<String> id = new Vector<>();
        for (int i = 0; i < itemName.size(); i++)
            if (itemList.get(i).getType().equals("Cdevice"))
                id.add(itemName.get(i));
        return id;
    }

    // 获取所有底盘id
    public Vector<String> getChassisID() {
        Vector<String> id = new Vector<>();
        for (int i = 0; i < itemName.size(); i++)
            if (itemList.get(i).getType().equals("chassis"))
                id.add(itemName.get(i));
        return id;
    }

    // 获取所有雷达id
    public Vector<String> getRadarID() {
        Vector<String> id = new Vector<>();
        for (int i = 0; i < itemName.size(); i++)
            if (itemList.get(i).getType().equals("radar"))
                id.add(itemName.get(i));
        return id;
    }

    public Vector<String> getSEID() {
        Vector<String> id = new Vector<>();
        for (int i = 0; i < itemName.size(); i++)
            if (itemList.get(i).getType().equals("se"))
                id.add(itemName.get(i));
        return id;
    }

    public Vector<String> getWantedPic(){
        Vector<String> id = new Vector<>();
        for (int i = 0; i < itemName.size(); i++)
            if (itemList.get(i).getType().equals("wantedPic"))
                id.add(itemName.get(i));
        return id;
    }

    // 检测是否存在
    public boolean isItemRepeat(String name) {
        for (int i = 0; i < itemName.size(); i++)
            if (itemName.get(i).equals(name))
                return true;
        return false;
    }

    // 根据num获取itemname
    public String getNameOfNum(int num) {
        return itemName.get(num);
    }

    // 根据num获取item
    public FCItem getItemOfNum(int num) {
        return itemList.get(num);
    }

    // 根据name获取物品
    public FCItem getItemOfID(String name) {
        if (name == null) return null;
        if ("null".equals(name)) return null;
        if (extraItemCloneImpl != null) {
            if (name.contains(",")) {
                //TODO 有，号的都有克隆体备份的
                FCItem find =  extraItemCloneImpl.getCloneFcItem(name);
                if(find != null){
                    return find;
                }else {
                    return itemMaps.get(name.split(",")[0]);
                }
            }
        }
        return itemMaps.get(name);
    }

    public void setExtraItemCloneImpl(ExtraItemCloneImpl extraItemClone) {
        extraItemCloneImpl = extraItemClone;
    }

}
